const app = getApp();
const pageSize = 20;
const typeList = [{
    id: 999,
    label: '全部'
  },
  {
    id: 0,
    label: '普通卡'
  },
  {
    id: 1,
    label: '老年卡'
  },
  {
    id: 2,
    label: '婴幼儿卡'
  },
  {
    id: 3,
    label: '贫困卡'
  }];
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    record_data: [],
    conditions: {
      start_time: '',
      end_time: '',
      typeIndex: '',
      card: '',
    },
    typeList,
    loadmore: false,
    nomore: false,
    isFilter: false,
    drawer: false,
    empty: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.waterCardAuditList();
    this.getCarType();
  },

  onReachBottom: function () {
    if (this.data.loadmore) {
      this.setData({
        page: this.data.page + 1
      })
      this.waterCardAuditList();
    }
  },

  onPullDownRefresh: function () {
    this.reloadData();
  },

  reloadData() {
    this.setData({
      page: 1,
      record_data: [],
      nomore: false,
      loadmore: false
    })
    wx.showLoading();
    this.waterCardAuditList();
  },

  // 修改
  modifyBtn: function (e) {
    wx.navigateTo({
      url: '../promotersModify/promotersModify?id=' + e.currentTarget.dataset.id + '&source_type=' + e.currentTarget.dataset.source_type,
    })
  },



  waterCardAuditList() {

    app.ajax({
      url: 'Partner/WaterCard/waterCardAuditList',
      method: "POST",
      data: {
        page: this.data.page,
        row: pageSize,
        salesman_id: 'all',
        start_time: this.data.conditions.start_time,
        end_time: this.data.conditions.end_time,
        card: this.data.conditions.card,
        card_type: this.data.conditions.typeIndex !== "" ? this.data.typeList[this.data.conditions.typeIndex].id : ""
      },
      success: res => {
        wx.hideLoading();
        wx.stopPullDownRefresh();
        var flag = res.data;
        if (flag.code > 0) {
          let source = res.data.data;
          let list = this.data.record_data.concat(source.data);
          this.setData({
            empty: source.page.total_number == 0 ? true : false,
            nomore: list.length >= source.page.total_number ? true : false,
            loadmore: list.length < source.page.total_number ? true : false,
            record_data: list,
            drawer: false,
          })
        } else {
          app.showToast(res.data.message);
        }
      },
      error: e => {
        wx.hideLoading();
        wx.stopPullDownRefresh();
        app.showToast("网络错误");
      }
    })
  },

  handleTime(e) {
    const name = e.currentTarget.dataset.name;
    this.setData({
      [`conditions.${name}`]: e.detail.value
    })
  },


  showDrawer() {
    this.condition = {
      ...this.data.conditions
    };
    this.setData({
      drawer: true,
    })
  },

  closeDrawer() {
    this.setData({
      drawer: false,
      conditions: this.condition
    })
    delete(this.condition)
  },

  resetDrawer() {
    const conditions = this.data.conditions;
    for (let x in conditions) {
      conditions[x] = "";
    }
    this.setData({
      conditions,
    })
  },

  confirmDrawer() {
    let isFilter = false;
    const conditions = this.data.conditions;
    for (let x in conditions) {
      conditions[x] !== "" && (isFilter = true);
    }
    this.setData({
      isFilter
    })
    delete(this.condition);
    this.reloadData();
  },


})